import { Component, OnInit } from '@angular/core';
import {Router,ActivatedRoute} from "@angular/router";
import {TrainingService} from "../../../my-service/training.service";
import {UtilService} from "../../../my-service/util.service";

@Component({
  selector: 'app-QR-code',
  templateUrl: './QR-code.component.html',
  styleUrls: ['./QR-code.component.css'],
})
export class QRCodeComponent implements OnInit {
 trainingID:string;
 inviteCode:string;
  projectName='wt';
  training:any;
 url:string;
  codeEnd=true;
  className:string;
  classId:string;
  constructor(
    private trainingService: TrainingService,
    private  route:Router,
    private  routerInfo:ActivatedRoute,
    private utilServe:UtilService
  ) { }

  ngOnInit() {
    this.trainingID = this.routerInfo.snapshot.params.ID;
    this.className=this.routerInfo.snapshot.parent.params.className;
    this.classId=this.routerInfo.snapshot.parent.params.classID;
    this.trainingService.getCodeBytrainingId(this.trainingID).then(res=>{
      if(res.status==1){
        this.inviteCode=res.code;
        let nowTime = this.utilServe.getCurrentTime();
        if(res.EndTime>=nowTime){
          this.codeEnd=false;
        }
      }
    });
    this.url = 'https://clientcreativitytraining.lessonplan.cn/home/start;id='+this.trainingID;
    this.trainingService.getTrainingByID(this.trainingID).then(res=>{
      if(res.status==1){
        this.training= res.data[0];
      }
    })
  }
  //创建邀请码
  createCode(){
    let endTime = new Date();
    endTime.setMonth(endTime.getMonth() + 6); //设置结束时间为6个月以后
    let LibQuery=`;className=${this.className};classID=${this.classId}`;
    let data={
      projectName:this.projectName,
      guid:this.training.PK_TrainingGuid,
      FK_ClassGuid:this.classId,
      endTime,
      LibQuery
    };
    this.trainingService.createCode(data).then(res=>{
      if(res.status==1){
        this.inviteCode = res.code;
        this.codeEnd=false;
      }
    });
  }
}
